﻿@{
    ViewData["Title"] = "网站栏目设置";
    Layout = "~/Views/Shared/_ManageLayout.cshtml";
}
@section css{
    <style>
        .layui-table-cell {
            line-height: 35px;
        }
    </style>
}
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">关键字：</label>
                        <div class="layui-input-inline mr0">
                            <input id="edtSearchAuth" name="edtSearchAuth" class="layui-input" type="text" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" id="btnSearchAuth">
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button class="layui-btn icon-btn" onclick="window.location.reload()">刷新
                        </button>
                        <a class="layui-btn icon-btn" id="addClass">
                            <i class="layui-icon">&#xe654;</i>新增栏目
                        </a>
                    </div>
                    <div class="layui-inline">
                        <button id="btnExpandAuth" class="layui-btn icon-btn">全部展开</button>
                        <button id="btnFoldAuth" class="layui-btn icon-btn">全部折叠</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" lay-even lay-skin="line" lay-size="lg" id="tableAuth" lay-filter="tableAuth"></table>
        </div>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="tableBarAuth">
    {{# if(!d.isLanguage){ }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    {{# } }}
</script>
<!-- js部分 -->
<script>
    layui.use(['admin', 'layer', 'form', "treetable", 'table', 'laydate', 'laytpl', 'upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laydate = layui.laydate;
        var admin = layui.admin;
        var laytpl = layui.laytpl;
        var upload = layui.upload;
        var treetable = layui.treetable;
        // 渲染表格
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'parentId',
            elem: '#tableAuth',
            url: "/api/ContentClass",
            cellMinWidth: 100,
            page: false,
            cols: [[
                { type: 'numbers', title: '#' },
                { field: 'className', title: '栏目名称', minWidth: 200 },
                { templet: '#tableBarAuth', title: '操作', align: 'center', minWidth: 120 }
            ]],
        });

        //监听工具条
        table.on('tool(tableAuth)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent == "edit") {
                var height = $(window).height() * 0.9;
                var width = $(window).width() * 0.9;
                var check = layer.open({
                    type: 2,
                    title: '栏目详情',
                    maxmin: true,
                    shadeClose: true, //点击遮罩关闭层
                    area: [width + 'px', height + 'px'],
                    content: '/manage/ContentClassEdit?classId=' + data.id,
                });
            } else if (layEvent == "del") {
                layer.confirm('确定要删除“[' + data.className + ']”吗,删除后，可能会出现网站信息不全？', {
                    skin: 'layui-layer-admin',
                    shade: .1
                }, function (i) {
                    layer.load(2);
                    $.ajax({
                        url: '/api/ContentClass/' + data.id,
                        type: 'delete',
                        async: true,
                        dataType: 'json',
                        complete: function (obj) {
                            layer.closeAll('loading');
                        },
                        success: function (result) {
                            layer.closeAll('loading');
                            if (result.success) {
                                layer.msg(result.message, { icon: 1, time: 1000 }, function () {
                                    location.reload();
                                });
                            } else {
                                layer.msg(result.message, { icon: 2 });
                            }
                        }
                    });
                });
            }
        });
        //新增栏目
        $("#addClass").click(function () {
            var height = $(window).height() * 0.9;
            var width = $(window).width() * 0.9;
            var check = layer.open({
                type: 2,
                title: '栏目详情',
                maxmin: true,
                shadeClose: true, //点击遮罩关闭层
                area: [width + 'px', height + 'px'],
                content: '/manage/ContentClassEdit',
            });
        });


        // 搜索按钮点击事件
        $('#btnSearchAuth').click(function () {
            $('#edtSearchAuth').removeClass('layui-form-danger');
            var keyword = $('#edtSearchAuth').val();
            var $tds = $('#tableAuth').next('.treeTable').find('.layui-table-body tbody tr td');
            $tds.css('background-color', 'transparent');
            if (!keyword) {
                layer.tips('请输入关键字', '#edtSearchAuth', { tips: [1, '#ff4c4c'] });
                $('#edtSearchAuth').addClass('layui-form-danger');
                $('#edtSearchAuth').focus();
                return;
            }
            var searchCount = 0;
            $tds.each(function () {
                if ($(this).text().indexOf(keyword) >= 0) {
                    $(this).css('background-color', '#FAE6A0');
                    if (searchCount == 0) {
                        $('body,html').stop(true);
                        $('body,html').animate({ scrollTop: $(this).offset().top - 150 }, 500);
                    }
                    searchCount++;
                }
            });
            if (searchCount == 0) {
                layer.msg("没有匹配结果", { icon: 5, anim: 6 });
            } else {
                treetable.expandAll('#tableAuth');
            }
        });

        $('#btnExpandAuth').click(function () {
            treetable.expandAll('#tableAuth');
        });

        $('#btnFoldAuth').click(function () {
            treetable.foldAll('#tableAuth');
        });

    });
</script>

